package mwt.xml.xdbforms.schemalayer.types;

import javax.xml.XMLConstants;
import mwt.xml.xdbforms.dblayer.metadata.impl.ColumnMetaData;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;

public class XmlYear extends XmlDate {

    public XmlYear(Document doc) {
        super(doc);
    }

    XmlYear() {
        
    }

    /**
     * Il tipo YEAR verrà definito a seconda del range
     * presente in MySQL - 1901 - 2155
     * @param cmd
     */
    private void buildFragment(ColumnMetaData cmd) {

        Element xtype, restriction, maxInclusive, minInclusive;

        typeName = cmd.getTypeName();

        fragment = doc.createDocumentFragment();

        xtype = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI,"xs:simpleType");
        xtype.setAttribute("name", typeName);

        cmd.setXschemaTypeName(typeName);


        restriction = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI,"xs:restriction");
        restriction.setAttribute("base", "xs:gYear");

        maxInclusive = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI,"xs:maxInclusive");
        maxInclusive.setAttribute("value", "2155");
        minInclusive = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI,"xs:minInclusive");
        minInclusive.setAttribute("value", "1901");


        xtype.appendChild(restriction);
        restriction.appendChild(maxInclusive);
        restriction.appendChild(minInclusive);
        fragment.appendChild(xtype);



    }

    @Override
    public DocumentFragment getSchemaType(ColumnMetaData cmd) {

        buildFragment(cmd);
        return fragment;

    }

    @Override
    public Object getXSchemaToJavaType(String baseType, String value) {
        Integer i  = Integer.parseInt(value);
        return i;
    }


}

